package huixue.cn.com.rxapp.rx;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;

public class MaterialDesign3Activity extends SimpleActivity {

    private AppBarLayout appBar;

    private View include_toolbar_open;

    private View include_toolbar_close;

    private View bg_toolbar_open;

    private View bg_toolbar_close;

    private View bg_content;

    @Override
    public int getLayout() {
        return R.layout.activity_material_design3;
    }

    @Override
    public void initView() {
        appBar = findViewById(R.id.app_bar);
        include_toolbar_open = findViewById(R.id.include_toolbar_open);
        include_toolbar_close = findViewById(R.id.include_toolbar_close);

        bg_toolbar_open = findViewById(R.id.bg_toolbar_open);
        bg_toolbar_close = findViewById(R.id.bg_toolbar_close);

        bg_content = findViewById(R.id.bg_content);

    }

    @Override
    public void addEvent() {
        appBar.addOnOffsetChangedListener(onOffsetChangedListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        appBar.removeOnOffsetChangedListener(onOffsetChangedListener);
    }

    AppBarLayout.OnOffsetChangedListener  onOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            //垂直方向偏移量
            int offset = Math.abs(verticalOffset);

            //最大偏移距离
            int scrollRange = appBarLayout.getTotalScrollRange();

            //当滑动没超过一半，展开状态下toolbar显示内容，根据收缩位置，改变透明值
            if (offset <= scrollRange / 2) {
                include_toolbar_open.setVisibility(View.VISIBLE);
                include_toolbar_close.setVisibility(View.GONE);
                //根据偏移百分比 计算透明值
                float scale2 = (float) offset / (scrollRange / 2);
                int alpha2 = (int) (255 * scale2);

                bg_toolbar_open.setBackgroundColor(Color.argb(alpha2, 25, 131, 209));

            }else{
                include_toolbar_close.setVisibility(View.VISIBLE);
                include_toolbar_open.setVisibility(View.GONE);

                float scale3 = (float) (scrollRange - offset) / (scrollRange / 2);
                int alpha3 = (int) (255 * scale3);
                bg_toolbar_close.setBackgroundColor(Color.argb(alpha3, 25, 131, 209));
            }

            //根据偏移百分比计算扫一扫布局的透明度值
            float scale = (float) offset / scrollRange;
            int alpha = (int) (255 * scale);
            bg_content.setBackgroundColor(Color.argb(alpha, 25, 131, 209));
        }
    };
}
